//
//  CZMessionCenterTableViewCell.m
//  nsj_iOS
//
//  Created by apple on 2016/12/8.
//  Copyright © 2016年 GCZ. All rights reserved.
//

#import "CZMessionCenterTableViewCell.h"
#import "CZMessionCenterCollectionViewCell.h"
#import "CZMessionCenterCartoonAlbumView.h"
#import "CZMessionCenterViewController.h"

#import "CZDoujinDetailViewController.h"
static NSString *const cz_item_cell = @"cz_item_cell";


@interface CZMessionCenterTableViewCell () <UICollectionViewDelegate, UICollectionViewDataSource>

@end

@implementation CZMessionCenterTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (NSArray *)albumArray {
    if (!_albumArray) {
        _albumArray = [[NSArray alloc] init];
    }
    return _albumArray;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self) {
        self.backgroundColor = GLOBAL_BACKGROUND_COLOR;
        UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc]init];
        layout.minimumLineSpacing = 10;
        layout.minimumInteritemSpacing = 10;
        
        CGFloat itemW = (Main_Screen_Width - 10) / 2;
        CGFloat itemH = itemW * 0.6;
        layout.itemSize = CGSizeMake(itemW, itemH);
        
        //定义collectionview的信息
        UICollectionView *collectionView= [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, Main_Screen_Width, (Main_Screen_Width - 10) / 2 * 0.6 * 3 + 30) collectionViewLayout:layout];
        
        collectionView.bounces = NO;
        collectionView.backgroundColor=[UIColor whiteColor];
        collectionView.backgroundColor = GLOBAL_BACKGROUND_COLOR;
        collectionView.delegate = self;
        collectionView.dataSource = self;
        //注册单元格
        [collectionView registerClass:[CZMessionCenterCollectionViewCell class] forCellWithReuseIdentifier:cz_item_cell];
        [self.contentView addSubview:collectionView];
        _collectionView = collectionView;
    }
    
    return self;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 6;
}

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    CZMessionCenterCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cz_item_cell forIndexPath:indexPath];
    
    if (self.albumArray.count == 6) {
        NSDictionary *dic = self.albumArray[indexPath.row];
        if (![dic[@"share_pics"] isKindOfClass:[NSNull class]] && [(NSString *)dic[@"share_pics"] containsString:@"http"]) {
            cell.albumView.contentImageView.yy_imageURL = [NSURL URLWithString:[dic[@"share_pics"] stringByAppendingString:@"?x-oss-process=style/ios_doujin_50"]];
        }
        if (![dic[@"logo"] isKindOfClass:[NSNull class]] && [(NSString *)dic[@"logo"] containsString:@"http"]) {
            cell.albumView.userIcon.yy_imageURL = [NSURL URLWithString:dic[@"logo"]];
        } else {
            cell.albumView.userIcon.image = [UIImage imageNamed:@"logo"];
        }
        
        if (![dic[@"author"] isKindOfClass:[NSNull class]] &&[(NSString *)dic[@"author"] length] > 0) {
            cell.albumView.authorLabel.text = [NSString stringWithFormat:@"作者:%@", dic[@"author"]];
        }
    }

    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    CZDoujinDetailViewController *detailVC = [[CZDoujinDetailViewController alloc] init];
    @weakify(self);
    detailVC.backBlock = ^(BOOL success) {
        if (success) {
            [weak_self.homeVC getAlbumData];
        }
    };
    NSDictionary *dic = self.albumArray[indexPath.row];
    detailVC.dataDic = dic;
    [self.homeVC.navigationController pushViewController:detailVC animated:YES];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
